#!/usr/bin/env bash
if [ ! -d "./dist/" ];then
  mkdir ./dist
else
  rm -rf ./dist/*
fi

json=`cat ./src/pages.json`

pkg_pages=`echo $json | jq '.pages'`
pkg_public=`echo $json | jq '.public'`
pkg_home=`echo $json | jq '.home.pageNo'`

##### 拷贝公共资源 #####
public_length=`echo $pkg_public | jq  '.|length'`

for idx in `seq 0 ${public_length}`
do
  public_name=`echo $pkg_public | jq -r ".[$idx].name"`

  if [ "$public_name" != null ];then
    if [ "$public_name" = "floder" ];then
      cp -r ./src/$public_name ./dist/$public_name
    else
      cp -r ./src/$public_name ./dist/$public_name
    fi
  fi
done

##### 拷贝页面 #####
pkg_pages_length=`echo $pkg_pages | jq  '.|length'`

for idx in `seq 0 ${pkg_pages_length}`
do
  pangeNo=`echo $pkg_pages | jq -r ".[$idx].pangeNo"`
  pageTitle=`echo $pkg_pages | jq -r ".[$idx].title"`
  file=`echo $pkg_pages | jq -r ".[$idx].file"`
  nextPage=`echo $pkg_pages | jq -r ".[$idx].nextPage"`
  prevPage=`echo $pkg_pages | jq -r ".[$idx].prevPage"`

  echo \############ page$pangeNo.html \############

  if [ "$file" != null ];then
    cp ./src/$file ./dist/page$pangeNo.html

    sed -i '' -e '/id="button-home"/s@<a [^>]*>@<a id="button-home" class="button" href="page'$pkg_home'.html">@' ./dist/page$pangeNo.html

    if [ "$nextPage" != null ];then
      line=$(grep -n '</body>' ./dist/page$pangeNo.html | tail -1 | cut -d: -f1)
      sed -i '' "${line}s@</body>@<a class=\"button_next\" href=\"page$nextPage.html\"></a>&@" ./dist/page$pangeNo.html
    fi

    if [ "$prevPage" != null ];then
      line=$(grep -n '</body>' ./dist/page$pangeNo.html | tail -1 | cut -d: -f1)
      sed -i '' "${line}s@</body>@<a class=\"button_prev\" href=\"page$prevPage.html\"></a>&@" ./dist/page$pangeNo.html
    fi

    if [ "$pageTitle" != null ];then
      sed -i '' 's/<title>.*<\/title>/<title>'$pageTitle'<\/title>/' ./dist/page$pangeNo.html
    fi

    subPages=`echo $pkg_pages | jq -r ".[$idx].subPages"`
    subPageCount=`echo $subPages | jq  '.|length'`

    for subIdx in `seq 0 ${subPageCount}`
    do
      navigateTo=`echo $subPages | jq -r ".[$subIdx].navigateTo"`
      targetId=`echo $subPages | jq -r ".[$subIdx].targetId"`

      line=$(grep -n 'id=\"'$targetId'\"' ./dist/page$pangeNo.html | tail -1 | cut -d: -f1)

      if [ "$line" != null -a "$line" != "" ];then
        herf="page$navigateTo.html"

        line_content=$(sed -n "${line}p" ./dist/page$pangeNo.html)

        # 检查行内容是否包含 href
        if [[ $line_content == *"href=\""* ]]; then
            # 替换 href 的值
            new_line=$(echo "$line_content" | sed "s@href=[\"'][^\"']*['\"]@href=\"page$navigateTo.html\"@")
        else
            # 添加 href 属性
            new_line=$(echo $line_content | sed "s@<a @<a href=\"page$navigateTo.html\" @")
        fi

        sed -i '' "${line}s@.*@$new_line@" ./dist/page$pangeNo.html
      fi
    done
  fi
done

##### 设置目录按钮 #####
for idx in `seq 0 ${pkg_pages_length}`
do
  pangeNo=`echo $pkg_pages | jq -r ".[$idx].pangeNo"`
  file=./dist/page$pangeNo.html

  subPages=`echo $pkg_pages | jq -r ".[$idx].subPages"`
  subPageCount=`echo $subPages | jq  '.|length'`

  for subIdx in `seq 0 ${subPageCount}`
  do
    navigateTo=`echo $subPages | jq -r ".[$subIdx].navigateTo"`
    nextPage=$navigateTo

    echo \############  page$pangeNo.html  \############

    # 获取下一页
    while [ $nextPage != null -o $nextPage = $pkg_pages_length ]
    do

      end_page=`echo $pkg_pages | jq -r ".[$pkg_pages_length-1].pangeNo"`

      echo \### $nextPage

      looped=0

      for idx in `seq 0 ${pkg_pages_length}`
      do
        page_no=`echo $pkg_pages | jq -r ".[$idx].pangeNo"`

        if [ $nextPage = $page_no ]; then
          subPageFile=./dist/page$page_no.html
          line=$(grep -n 'id=\"button-catalogue\"' $subPageFile | tail -1 | cut -d: -f1)

          if [ "$line" != null -a "$line" != "" ];then
            herf="page$pangeNo.html"
            line_content=$(sed -n "${line}p" $subPageFile)

            # 检查行内容是否包含 href
            if [[ $line_content == *"href=\""* ]]; then
              # 替换 href 的值
              new_line=$(echo "$line_content" | sed "s@href=[\"'][^\"']*['\"]@href=\"page$pangeNo.html\"@")
            else
              # 添加 href 属性
              new_line=$(echo $line_content | sed "s@<a @<a href=\"page$pangeNo.html\" @")
            fi

            sed -i '' "${line}s@.*@$new_line@" $subPageFile
          fi

          nextPage=`echo $pkg_pages | jq -r ".[$idx].nextPage"`
          looped=1
          break
        fi
      done

      if [ $looped = 0 ];then
        break
      fi

    done
  done
done
